# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        博智科技  
# Name:         demo1021
# Description:  自定义序列
# Author:       yzl
# Date:         2019-02-07
#-------------------------------------------------------------------------------

class FactorialDict:
    def __init__(self):
        self.numDict = {}

    def factorial(self,n):
        # 递归调用终止条件
        if n == 0 or n == 1:
            return 1
        else:
            return n * self.factorial(n - 1)

    def __getitem__(self, key):
        print("__getitem__方法被调用,key={}".format(key))
        # 遍历 key
        if key in self.numDict:
            return self.factorial(self.numDict[key])
        else:
            return 0

    def __setitem__(self, key, value):
        # 设置键值
        print("__setitem__方法被调用,key={}".format(key))
        self.numDict[key] = int(value)

    def __delitem__(self, key):
        # 删除键值
        print("__delitem__方法被调用,key={}".format(key))
        del self.numDict[key]

    def __len__(self):
        # 返回字典长度
        print("__len__被调用")
        return len(self.numDict)


if __name__ == '__main__':
    d = FactorialDict()

    d['4!'] = 4
    # d['7!'] = 7
    # print(d.numDict)
    for item in d.numDict.keys():
        print(item)
    print('4!','=',d['4!'])
    print('7!','=',d['7!'])
    print(len(d))
    # del d['7!']
    # print(len(d))
